<html>
<head><meta charset="utf-8"><title>proptest: numbers that sum to a number · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html">proptest: numbers that sum to a number</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="168814934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168814934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168814934">(Jun 24 2019 at 00:55)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span> I’d like to write a proptest that takes as input a <code>Vec&lt;u8&gt;</code> and a <code>Vec&lt;usize&gt;</code> such that the sum of the latter equals the length of the former. This is for testing that processing a blob of bytes in arbitrary chunks does not affect my algorithm. Any suggestions on how to create that?</p>



<a name="168814992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168814992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168814992">(Jun 24 2019 at 00:56)</a>:</h4>
<p>hmm; well avoid filtering <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="168815045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815045">(Jun 24 2019 at 00:58)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> So if I understand the test correctly, I would generate the <code>vu: Vec&lt;usize&gt;</code>, fetch the length, and then generate a random <code>Vec&lt;u8&gt;</code> from that</p>



<a name="168815054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815054">(Jun 24 2019 at 00:59)</a>:</h4>
<p>hmm, do it backwards. Interesting.</p>



<a name="168815059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815059">(Jun 24 2019 at 00:59)</a>:</h4>
<p>Yeah because constructing a sum from a length seems... harder?</p>



<a name="168815062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815062">(Jun 24 2019 at 00:59)</a>:</h4>
<p>Tho possibly cheaper</p>



<a name="168815138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815138">(Jun 24 2019 at 01:02)</a>:</h4>
<p>Naively, constructing a sum from a length sounds like iteratively and randomly generating numbers that is within the interval of the length and then  adding them up until the sum is zero</p>



<a name="168815177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815177">(Jun 24 2019 at 01:02)</a>:</h4>
<p>Maybe proptest has something for that but I don't remember ^^</p>



<a name="168815305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815305">(Jun 24 2019 at 01:07)</a>:</h4>
<p>yeah, it felt like an iterative/recursive concept to me: pick a random number 0..len, repeat until 0, but I couldn’t figure out how to bend the recursive functionality</p>



<a name="168815365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815365">(Jun 24 2019 at 01:08)</a>:</h4>
<p>I would do some searching in the docs to find what you want :D</p>



<a name="168815371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815371">(Jun 24 2019 at 01:09)</a>:</h4>
<p>or make a PR if you didn't find it</p>



<a name="168815428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815428">(Jun 24 2019 at 01:11)</a>:</h4>
<p>you mean an issue? Hard to PR if I don’t know what the code is :-)</p>



<a name="168815542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815542">(Jun 24 2019 at 01:14)</a>:</h4>
<p>Amusingly, this “write it backwards” advice was something I read recently in a protesting article. Too bad I didn’t connect the dots</p>



<a name="168815607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815607">(Jun 24 2019 at 01:16)</a>:</h4>
<blockquote>
<p>thread 'same_results_as_c_for_64_bit_with_varying_chunks' panicked at 'capacity overflow', src/liballoc/raw_vec.rs:748:5</p>
</blockquote>
<p>Stupid property-based testing… testing all the properties</p>



<a name="168815842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815842">(Jun 24 2019 at 01:24)</a>:</h4>
<p>Yea figure out the code first :D</p>



<a name="168815861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168815861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168815861">(Jun 24 2019 at 01:25)</a>:</h4>
<p>Well, that was because I was creating a <code>Vec&lt;u8&gt;</code> with a Very Large Capacity via the props</p>



<a name="168816279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/proptest%3A%20numbers%20that%20sum%20to%20a%20number/near/168816279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/proptest.3A.20numbers.20that.20sum.20to.20a.20number.html#168816279">(Jun 24 2019 at 01:39)</a>:</h4>
<p>In case you are interested, this article calls this technique an “oracle generator”</p>
<p><a href="https://wickstrom.tech/programming/2019/04/17/property-based-testing-in-a-screencast-editor-case-study-2.html" target="_blank" title="https://wickstrom.tech/programming/2019/04/17/property-based-testing-in-a-screencast-editor-case-study-2.html">https://wickstrom.tech/programming/2019/04/17/property-based-testing-in-a-screencast-editor-case-study-2.html</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>